/**
 * Copyright (c) 2025 myons Inc. All rights reserved.
 */
package cn.zmwh.im.server.netty;

import cn.zmwh.im.server.constant.BsKImConstant;
import io.netty.channel.Channel;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;

/**
 * @describe: 通道 、
 * @author: dmzmwh 、
 * @time: 2025-06-13 19:21 、
 */
@Slf4j
public class ChannelUtil {


    public static void close(Channel channel, String location) {
        if (channel != null && channel.isActive()) {
            AttributeKey<Long> userIdAttr = AttributeKey.valueOf(BsKImConstant.CHANNEL_ATTR_KEY.USER_ID);
            Long uid = channel.attr(userIdAttr).get();
            // 关闭channel
            channel.close().addListener(future -> {
                if (future.isSuccess()) {
                    log.info("用户：{}因为：{}，主动关闭连接", uid, location);
                } else {
                    log.info("用户：{}因为：{}，主动关闭连接失败", uid, location, future.cause());
                }
            });
        }
    }
}
